//
//  AppDelegate.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/16.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "AppDelegate.h"
#import "QSMainTabBarController.h"
#import "QSUserInterface.h"
#import "TYAlertController.h"
@interface AppDelegate ()

@end

static NSString * const kIngnoreUpdateVersionKey = @"kIngnoreUpdateVersionKey";
@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self startNetworkReachability];
    [self initUMengShare];

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
       
    self.window = [[UIWindow alloc] initWithFrame:kScreenBounds];
    [self checkLoginWithRootVc];
    [self.window makeKeyAndVisible];
    [self startWindowAnimation];
    [self getNeedUpadateApi];
    return YES;
}

-(void)startWindowAnimation{
     NSDictionary * dict = @{@"320x480" : @"LaunchImage-700", @"320x568" : @"LaunchImage-700-568h", @"375x667" : @"LaunchImage-800-667h", @"414x736" : @"LaunchImage-800-Portrait-736h",@"375x812" : @"LaunchImage750",@"414x896":@"LaunchImage"};
    NSString * key = [NSString stringWithFormat:@"%dx%d", (int)[UIScreen mainScreen].bounds.size.width, (int)[UIScreen mainScreen].bounds.size.height];
    NSLog(@"key:%@", key);
    UIImage * launchImage = [UIImage imageNamed:dict[key]];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    imageView.image = launchImage;
    
//    self.window.userInteractionEnabled = NO;
    [self.window addSubview:imageView];
    [self.window bringSubviewToFront:imageView];
    [UIView animateWithDuration:2 animations:^{
        imageView.alpha = 0;
        
        NSArray *typeArray = @[kCATransitionFade, kCATransitionPush, kCATransitionMoveIn, kCATransitionReveal, @"cube", @"suckEffect", @"oglFlip", @"rippleEffect", @"pageCurl", @"pageUnCurl", @"cameraIrisHollowOpen", @"cameraIrisHollowClose"];
        CATransition *animation = [CATransition animation];
        animation.duration = 0.1;
        animation.type = typeArray[3];
        animation.subtype = kCATransitionFromBottom;
        [imageView.layer addAnimation:animation forKey:nil];
    } completion:^(BOOL finished) {
        [imageView removeFromSuperview];
//        self.window.userInteractionEnabled = YES;

    }];
}

- (void)initUMengShare {
    // 配置友盟SDK产品并并统一初始化
    // [UMConfigure setEncryptEnabled:YES]; // optional: 设置加密传输, 默认NO.
    // [UMConfigure setLogEnabled:YES]; // 开发调试时可在console查看友盟日志显示，发布产品必须移除。
    
    [UMConfigure initWithAppkey:kUMAppKey channel:@"App Store"];
    /* appkey: 开发者在友盟后台申请的应用获得（可在统计后台的 “统计分析->设置->应用信息” 页面查看）*/
    DLog(@"友盟版本号：%ld",(long)[UMConfigure version]);
    // 请参考「Share详细介绍-初始化第三方平台」
    // 分享组件配置，因为share模块配置可选三方平台较多，代码基本跟原版一样，也可下载demo查看
    //设置微信的appKey和appSecret
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:kWeChatAppKey appSecret:kWechatAppSecret redirectURL:@"http://mobile.umeng.com/social"];
    //设置分享到QQ互联的appKey和appSecret
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:kQQAppKey  appSecret:kQQAppSecret redirectURL:@"http://mobile.umeng.com/social"];
    //设置新浪的appKey和appSecret
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:kWeiBoAppKey  appSecret:kWeiBoAppSecret redirectURL:@"http://www.weibo.com"];
    [UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
}

- (void)startNetworkReachability {
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        switch (status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                DLog(@"未知网络");
                break;
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
                DLog(@"没有网络(断网)");
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                DLog(@"手机自带网络");
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                DLog(@"WIFI");
                break;
        }
    }];
    // 3.开始监控
    [mgr startMonitoring];
}


- (void)checkLoginWithRootVc{
    if (kUserLogin) {
        self.window.rootViewController = [[QSMainTabBarController alloc] init];
    } else {
        self.window.rootViewController = [[QSBaseNavigationController alloc] initWithRootViewController:[[QSLoginViewController alloc] init]];
    }
}
//是否需要更新版本
- (void)getNeedUpadateApi{
    [QSUserInterface getNeedUpdateWithAppFlag:@"2" Version:kCurrentVersion andBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, QSNeedUpdateVersionModel * _Nonnull result) {
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            NSLog(@"当前版本号：%@", kCurrentVersion);
            NSLog(@"最新版版本号：%@", result.version);
            if (result.isNow) {
                [TYAlertController showAlertViewWithTitle:@"检测到有版本更新" message:result.content confirmTitle:@"前往更新" confirmAction:^{
                    //跳转AppStore
                    QSOpenURL(AppStoreUrl);
                }];
            }
            //当前版本已经忽略更新 则不提示
            NSString *ingnoreVersion = [QSUserDefaults objectForKey:kIngnoreUpdateVersionKey];
            if ([ingnoreVersion isEqualToString:kCurrentVersion]) {
                return;
            }
            //如果需要更新
            if (result.isUpdate) {
                [TYAlertController showAlertViewWithTitle:@"检测到有版本更新" message:result.content confirmTitle:@"前往更新" cancelTitle:@"暂不更新" confirmAction:^{
                    [QSUserDefaults setObject:kCurrentVersion forKey:kIngnoreUpdateVersionKey];
                    //跳转AppStore
                    QSOpenURL(AppStoreUrl);
                } cancelAction:^{
                    //记录忽略更新的版本 下次在提示
                    [QSUserDefaults setObject:kCurrentVersion forKey:kIngnoreUpdateVersionKey];
                }];
            }
            
        }
    }];
}

@end
